package com.aiyan.edu.framework.interceptor;

import com.aiyan.edu.framework.canary.context.CanaryContextHolder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * HandlerInterceptorAdapter实现
 * <p>
 * 获取来自上游请求的头
 * 这里主要获取灰度标签头设置到HystrixContext上下文，负载均衡时使用
 *
 * @author: zqzhou
 * @create: 2019/12/05 22:52
 **/
public class CanaryHeaderInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        CanaryContextHolder.setCanaryContext(request.getHeader(CanaryContextHolder.CANARY_META_KEY));
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        CanaryContextHolder.destroy();
    }
}
